package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.TongZhiBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.SwipeRefreshHelper;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;
import com.mm.dss.demo.view.LogoutDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class TongZhiActivity extends BaseActivity {
    TextView txt_title,right;
    RelativeLayout layout_back;
    RecyclerView recyclerView;//
    BaseRecyclerAdapter<TongZhiBean> baseRecyclerAdapter;
    List<TongZhiBean> tongZhiBeanlist=new ArrayList<>();
    private Dialog mWeiboDialog;
    private SwipeRefreshLayout swipregresh;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tong_zhi);
        txt_title= (TextView) findViewById(R.id.txt_title);
        right=(TextView) findViewById(R.id.right);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        txt_title.setText("通知公告");
        right.setText("新建通知");
        layout_back.setVisibility(View.VISIBLE);
        if (SharedPreferencedUtils.getString(TongZhiActivity.this,"Level").equals("1")){
            right.setVisibility(View.VISIBLE);
        }else {
            right.setVisibility(View.GONE);
        }

        swipregresh= (SwipeRefreshLayout) findViewById(R.id.swipregresh);
        initdata();
        initSwipeRefresh();
    }

    protected void initdata() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseRecyclerAdapter=new BaseRecyclerAdapter<TongZhiBean>
                (this,null,R.layout.item_tongzhiview) {
            @Override
            protected void convert(final BaseViewHolder helper, final TongZhiBean item) {
                helper.setText(R.id.txt_tongzhi,item.getBT());
                helper.setText(R.id.time,item.getSJ());
                if (SharedPreferencedUtils.getString(TongZhiActivity.this,"Level").equals("1")){
                    helper.getView(R.id.btnCancel).setVisibility(View.VISIBLE);
                }else {
                    helper.getView(R.id.btnCancel).setVisibility(View.GONE);
                }
                helper.getView(R.id.btnCancel).setOnClickListener(v -> {
                    //退出登录
                    LogoutDialog dialog = new LogoutDialog(TongZhiActivity.this);
                    dialog.setTltMsg("是否删除该公告");
                    dialog.setNoStr("取消");
                    dialog.setYesStr("删除");
                    dialog.setYesTextColor(R.color.logout);
                    dialog.setNoOnclickListener(view -> {
                        dialog.dismiss();
                    });
                    dialog.setYesOnclickListener(view -> {
                        dialog.dismiss();
                        Cancel(item.getID());
                    });
                    dialog.show();

                });
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(TongZhiActivity.this,TongzhiInfoActivity.class);
                        intent.putExtra("title",item.getBT());
                        intent.putExtra("content",item.getNR());
                        intent.putExtra("ID",item.getID());
                       startActivity(intent);
                    }
                });
            }
        };
        baseRecyclerAdapter.openLoadAnimation(false);
        recyclerView.setAdapter(baseRecyclerAdapter);



    }

    @Override
    protected void onResume() {
        super.onResume();
        setnet(true);
    }

    private void setnet(boolean IsDialog) {
        if (tongZhiBeanlist.size()>0){
            tongZhiBeanlist.clear();
        }
        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(TongZhiActivity.this,
                            "加载中...");
        }
        Map<String,String> map=new HashMap();
        map.put("userid",SharedPreferencedUtils.getString(TongZhiActivity.this,"username"));
        WebServiceUtils.callWebService(Constances.webhosturl, "N03_GetGonggao", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                swipregresh.setRefreshing(false);
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                finishRefresh();
                Log.i("通知接口",result.getProperty(0).toString());
                try {
                    JSONArray array=new JSONArray(result.getProperty(0).toString());
                    for (int i=0;i<array.length();i++){
                        Gson gson=new Gson();
                        TongZhiBean tongZhiBean=gson.fromJson(array.optJSONObject(i).toString(),TongZhiBean.class);
                        tongZhiBeanlist.add(tongZhiBean);
                    }

                    baseRecyclerAdapter.setData(tongZhiBeanlist);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void Cancel(String id) {
        if (tongZhiBeanlist.size()>0){
            tongZhiBeanlist.clear();
        }
        mWeiboDialog = WeiboDialogUtils.
                createLoadingDialog(TongZhiActivity.this,
                        "加载中...");
        Map<String,String> map=new HashMap();
        map.put("id",id);
        WebServiceUtils.callWebService(Constances.webhosturl, "DelGonggao", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                swipregresh.setRefreshing(false);
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                Log.i("通知接口",result.getProperty(0).toString());
                Toast.makeText(TongZhiActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                setnet(true);
            }
        });
    }
    public void onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                  finish();
                break;
            case R.id.right:
                Intent intent=new Intent(TongZhiActivity.this,NewTongZhiActivtity.class);
                startActivity(intent);
                break;
        }
    }
    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (swipregresh != null) {
            SwipeRefreshHelper.init(swipregresh, new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    setnet(false);

                }
            });
        }
    }

    /**
     * 关闭刷新
     */
    public void finishRefresh() {
        if (swipregresh != null) {
            swipregresh.setRefreshing(false);
        }
    }
}
